<?php
class User {
    public function show_alert($alert){
        if(isset($alert)){
            $types = array('success', 'warning', 'danger');
            if(strlen($alert[0]) > 0 && in_array($alert[1], $types)){
                echo '<p class="bg-'.$alert[1].' text-center">'.$alert[0].'</p>';
            }
        }
    }

    public function is_logged(){
        if(isset($_SESSION['uid']) && is_numeric($_SESSION['uid'])) return 1;
        else return 0;
    }

    public function is_leader(){
        if($this->is_logged()){
            $database = Database::getInstance();
            $leader_query = $database -> dbh -> prepare("SELECT leader FROM student WHERE id = '".$_SESSION['uid']."' LIMIT 1");
            if($leader_query->execute()) {
                $leader = $leader_query -> fetchColumn();
                if($leader == '1') return 1;
            }
            else return 0;
        }
    }

    public function is_admin(){
        if($this->is_logged()){
            $database = Database::getInstance();
            $leader_query = $database -> dbh -> prepare("SELECT leader FROM student WHERE id = '".$_SESSION['uid']."' LIMIT 1");
            if($leader_query->execute()) {
                $leader = $leader_query -> fetchColumn();
                if($leader == '2') return 1;
            }
            else return 0;
        }
    }

    public function check_login($login) // sprawdza, czy dany login jest wolny
    {
		$database = Database::getInstance();
		$check_query = $database->dbh->prepare("SELECT login FROM student WHERE login='{$login}'");
		$check_query->execute();
		if($check_query->rowCount() > 0){ 
			return 0; // znaleziono w bazie użytklownika z danym loginem
		}else{
			return 1; //jest wolny
		}
    }
	
	public function register_user($login,$password,$firstName,$lastName,$album,$mail,$kierunek,$year) // rejestruje uzytkownika
    {
		$database = Database::getInstance();
		$register_query = $database->dbh->prepare("INSERT INTO `student` (login, password, firstName, lastName, album, mail, direction, year) VALUES ('$login', '$password', '$firstName', '$lastName', '$album', '$mail', '$kierunek', '$year')");
		
		if($register_query->execute()){ 
			return 1; // zarejestrowano
		}else{
			return 0; // coś się nie udało
		}
    }
	
	public function get_id() // zwraca id uzytkownika
    {
		$database = Database::getInstance();
		$login = $_SESSION['login'];
		$register_query = $database->dbh->prepare("SELECT id FROM student WHERE login = '$login'");
		
		if($register_query->execute()){ 
			$id = $register_query->fetchColumn();
			return $id; // znaleziono id
		}else{
			return 0; // nie znaleziono
		}
    }
} 

?>